﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion


#region Usings

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Surface.Presentation;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.UI.View.Controls;
using ScrumTable.UI.View.Extension;
using ScrumTable.UI.View.Localization;
using ScrumTable.UI.View.Util;
using ScrumTable.UI.ViewModel;

#endregion
namespace ScrumTable.UI.View.WinTouch.UserControls
{
    /// <summary>
    /// Interaction logic for SurfaceUserControl1.xaml
    /// </summary>
    public partial class BugControl :  IDropUserControl
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        private readonly ClickUtil _click = new ClickUtil();
        private readonly ClickUtil _clickTouch = new ClickUtil();


        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
 


        #endregion

        #region Constructors / Destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="UserStoryControl"/> class.
        /// </summary>
        public BugControl()
        {
            DataContextChanged += OnDataContextChanged;
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                InitializeComponent();
                FontSize = 10;
                NameLabel.FontWeight = FontWeights.Normal;
            }
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
        private bool ShowBackGround(Point position, bool isTouch)
        {
            if (PresentationSource.FromVisual(this) == null || (isTouch ? !_clickTouch.IsClickInTimeAndPlace(position) : !_click.IsClickInTimeAndPlace(position)))
                return false;

            var bug = ((BugViewModel)DataContext).Original;
            if (bug == null) { return false; }
            var item = new BugControlBack
            {

                VerticalAlignment = VerticalAlignment.Bottom,
                HorizontalAlignment = HorizontalAlignment.Left,
                DataContext = DataContext
            };


           ShowItem(bug, item, position, LanguageDictionary.Current.Translate<string>("Bug_Titel_Back", "Content"));
           return true;
        }

       

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        private void OnUserStoryPreviewContactDown(object sender, TouchEventArgs e)
        {
            e.Handled = ShowBackGround(e.GetTouchPoint(this).Position, true);
        }


        private void OnBugMouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = ShowBackGround(e.GetPosition(this), false);
        }


       
        private void OnUserStoryDrop(object sender, SurfaceDragDropEventArgs e)
        {
            if (e.Cursor.Data is MemberViewModel)
                ((BugViewModel)DataContext).AssignedMember = ((MemberViewModel)e.Cursor.Data);
            else
                e.Effects = DragDropEffects.None;
        }

        private void OnUserStoryPreviewQueryTarget(object sender, QueryTargetEventArgs e)
        {
            if (CanDrop(e.Cursor.Data) != CanDropResult.CanDrop)
            {
                e.UseDefault = false;
                e.ProposedTarget = null;
                e.Handled = true;
            }
        }


        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (DataContext is Bug)
            {
                DataContext = new BugViewModel((Bug)DataContext);
            }
        }

        #endregion



        #region IDropUserControl Members

        public FrameworkElement GetRootControl
        {
            get { return DropRoot; }
        }

        public bool ShowIconWithbackground
        {
            get { return true; }
        }

        public CanDropResult CanDrop(object objectToDrop)
        {
            return this.ConvertBoolToEnum(objectToDrop is MemberViewModel);
        }

        #endregion
    }
}
